/*
 * AddressListPanel.java
 * 
 * Copyright 2006 Sun Microsystems, Inc. ALL RIGHTS RESERVED Use of 
 * this software is authorized pursuant to the terms of the license 
 * found at http://developers.sun.com/berkeley_license.html .
 *
 */

package org.knoppers.examples;

import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListSelectionListener;

/**
 * 
 */
public class AddressListPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6279187064980828706L;

	/** Creates new form AddressListPanel */
	@SuppressWarnings("rawtypes")
	public AddressListPanel() {
		renderer = new ListEntryRenderer();
		model = new DefaultListModel();
		initComponents();

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// desc=" Generated Code ">//GEN-BEGIN:initComponents
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void initComponents() {
		scrollPane = new JScrollPane();
		addressList = new JList();

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setHorizontalScrollBar(null);
		scrollPane.setViewport(null);
		addressList.setModel(model);
		addressList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		addressList.setCellRenderer(renderer);
		scrollPane.setViewportView(addressList);

		add(scrollPane);

	}// </editor-fold>//GEN-END:initComponents

	@SuppressWarnings("unchecked")
	public void addListEntry(ListEntry entry) {
		model.addElement(entry);

	}

	public void addListEntries(List<ListEntry> list) {
		for (ListEntry entry : list) {
			addListEntry(entry);
		}
	}

	public int getSelectedIndex() {
		return addressList.getSelectedIndex();
	}

	public int setSelectedIndex(int index) {
		assert (index >= -1);
		DefaultListModel<?> model = (DefaultListModel<?>) addressList.getModel();
		int size = model.getSize();
		if (index < size) {
			addressList.setSelectedIndex(index);
		} else {
			addressList.setSelectedIndex(size - 1);
			index = size - 1;
		}
		return index;
	}

	public ListEntry getSelectedListEntry() {
		ListEntry entry = (ListEntry) addressList.getSelectedValue();
		return entry;
	}

	public int deleteSelectedEntry() {
		int selectedIndex = addressList.getSelectedIndex();
		if (selectedIndex >= 0) {
			DefaultListModel<?> model = (DefaultListModel<?>) addressList.getModel();
			model.remove(selectedIndex);
		}
		return selectedIndex;
	}

	public void addListSelectionListener(ListSelectionListener listener) {
		addressList.addListSelectionListener(listener);
	}

	public void removeListSelectionListener(ListSelectionListener listener) {
		addressList.removeListSelectionListener(listener);
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private JList<?> addressList;
	private JScrollPane scrollPane;
	// End of variables declaration//GEN-END:variables

	private ListEntryRenderer renderer;
	@SuppressWarnings("rawtypes")
	private DefaultListModel model;
}
